package com.meilai.project.mapper.business.sale;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.meilai.project.entity.business.sale.BillSaleMediaDetail;
import com.meilai.project.vo.business.sale.BillSaleMediaDetailVO;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface BillSaleMediaDetailMapper extends BaseMapper<BillSaleMediaDetail> {

    @Delete("DELETE FROM bill_sale_media_detail WHERE bill_sale_id = #{billSaleId}")
    int deleteByBillSaleId(@Param("billSaleId") Long billSaleId);

    @Select("SELECT a.*,a.id as bill_sale_detail_id,b.name as media_type_name FROM bill_sale_media_detail a " +
            "LEFT JOIN media_type b ON a.media_type_id = b.id " +
            "WHERE a.bill_sale_id = #{billSaleId} ORDER BY a.id ASC ")
    List<BillSaleMediaDetailVO> selectByBillSaleId(@Param("billSaleId") Long billSaleId);

    @Select("SELECT a.*,a.id as bill_sale_detail_id,b.name as media_type_name FROM bill_sale_media_detail a " +
            "LEFT JOIN media_type b ON a.media_type_id = b.id " +
            "WHERE a.bill_sale_id = #{billSaleId} AND a.id = #{param1} ORDER BY a.id ASC ")
    List<BillSaleMediaDetailVO> getByBillSaleId(@Param("billSaleId") Long billSaleId, @Param("billSaleDetailId") Long billSaleDetailId);


    @Select("SELECT SUM(amount) FROM bill_sale_media_detail WHERE contract_id = #{contractId}")
    BigDecimal getTotalExecutedAmount(@Param("contractId") Long contractId);

    @Update("update bill_sale_media_detail set is_market = #{marketDetailId} where id = #{billSaleDetailId}")
    void updateIsMarket(@Param("marketDetailId") Long marketDetailId, @Param("billSaleDetailId") Long billSaleDetailId);

}
